package capricornus.components.utils.movieclip
{
	import capricornus.actor.res.ActionImage;
	import capricornus.core.RefreshManager;
	import capricornus.events.RefreshEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;

	public class MovieImages extends Sprite
	{
		private var _isClone:Boolean = false;
		
		private var _vecImage:Vector.<BitmapData>;
		private var _vecOffsetXY:Vector.<Array>;
		
		private var _isPlaying:Boolean = false;
		private var _frame:int = 0;
		private var _attachBitmap:Bitmap=null;
		
		public function MovieImages()
		{
			init();
		}
		
		private function init():void
		{
			_attachBitmap = new Bitmap();
			addChild( _attachBitmap );
		}
		
		public function destroy():void
		{
			stop();
			if( !_isClone )
			{
				while( _vecImage.length > 0 ) _vecImage.pop().dispose();
				while( _vecOffsetXY.length > 0 ) _vecOffsetXY.pop();
			}
			_isClone = true;
			
			if( _attachBitmap != null )
			{
				if( _attachBitmap.parent != null )
				{
					_attachBitmap.parent.removeChild( _attachBitmap );
				}
				_attachBitmap.bitmapData = null;
			}
			if( parent ) parent.removeChild( this );
		}
		
		public function clone():MovieImages
		{
			var mi:MovieImages = new MovieImages();
			mi._vecImage = _vecImage;
			mi._vecOffsetXY = _vecOffsetXY;
			mi._isClone = true;
			mi.refresh();
			
			return mi;
		}
		
		public function play():void
		{
			if( _isPlaying==false )
			{
				_isPlaying = true;
				RefreshManager.instance.addEventListener( RefreshEvent.REFRESH, onRefresh, false, 0, true );
			}
		}
		
		public function stop():void
		{
			if( _isPlaying )
			{
				_isPlaying = false;
				RefreshManager.instance.removeEventListener( RefreshEvent.REFRESH, onRefresh );
			}
		}
		
		public function nextFrame():void
		{
			_frame++;
			if( _frame < 0 ) _frame = 0;
			if( _frame >= _vecImage.length ) _frame = 0;
			refresh();
		}
		
		public function prevFrame():void
		{
			_frame--;
			if( _frame < 0 ) _frame = 0;
			if( _frame >= _vecImage.length ) _frame = 0;
			refresh();
		}
		
		/**
		 * 
		 * @param frame 帧(1索引)，帧名(暂时不支持);
		 * 
		 */		
		public function gotoAndPlay( frame:Object ):void
		{
			if( frame is int )
			{
				_frame = int(frame) - 1;
				if( _frame < 0 ) _frame = 0;
				if( _frame >= _vecImage.length ) _frame = _vecImage.length - 1;
				refresh();
				play();
			}
		}
		
		/**
		 * 
		 * @param frame 帧(1索引)；帧名(暂时不支持);
		 * 
		 */		
		public function gotoAndStop( frame:Object ):void
		{
			if( frame is int )
			{
				_frame = int(frame) - 1;
				if( _frame < 0 ) _frame = 0;
				if( _frame >= _vecImage.length ) _frame = _vecImage.length - 1;
				refresh();
				stop();
			}
		}
		
		/**
		 * 一般情况下不要用这个函数，因为bitmap的x、y会经常变化的
		 * @return 
		 * 
		 */		
		public function get bitmap():Bitmap
		{
			return _attachBitmap;
		}
		
		private function onRefresh( e:RefreshEvent ):void
		{
			refresh();
			
			_frame++;
			if( _frame >= _vecImage.length )
			{
				_frame = 0;
			}
		}
		
		private function refresh():void
		{
			if( _attachBitmap != null )
			{
				_attachBitmap.bitmapData = _vecImage[_frame];
				var arr:Array = _vecOffsetXY[_frame];
				_attachBitmap.x = int(arr[0]);
				_attachBitmap.y = int(arr[1]);				
			}
		}
		
		public static function createMovieImagesFromActionImage( ai:ActionImage ):MovieImages
		{
			var mi:MovieImages = new MovieImages();
			mi._vecImage = new Vector.<BitmapData>();
			mi._vecOffsetXY = new Vector.<Array>();
			return mi;
		}
		
		public static function createMovieImagesFromMoviclip( mc:MovieClip ):MovieImages
		{
			var mi:MovieImages = new MovieImages();
			
			mi._vecImage = new Vector.<BitmapData>();
			mi._vecOffsetXY = new Vector.<Array>();
			
			var mt:Matrix = new Matrix();
			var len:int = mc.totalFrames;
			for( var i:int=0; i<len; i++ )
			{
				mc.gotoAndStop( i+1 );
				
				var rect:Rectangle = mc.getBounds( mc );
				var bd:BitmapData = new BitmapData( rect.width, rect.height, true, 0 );
				mt.tx = -rect.x;
				mt.ty = -rect.y;
				bd.draw( mc, mt );
				
				mi._vecImage.push( bd );
				mi._vecOffsetXY.push( [int(rect.x), int(rect.y)] );
			}
			
			mi.refresh();
			return mi;
		}
	}
}